#include <iostream>
#include <vector>
#include <string>
using namespace std;
#include "game_widget/game_block.h"
#include "game_widget/game_reward.h"
#include "game_widget/game_background.h"
#include "game_widget/game_building.h"
using namespace UI;
#include <SDL/SDL_image.h>

class TestView : public UI_View{
private:
	UI_Timer* p_timer;
	Sence_Config* p_config;
	Game_Block* p_block;
	Game_Reward* p_reward;
	Game_Building* p_building;
	Game_Background* p_background;
public:
	TestView() : UI_View() {
		p_config = new Sence_Config("data/config/city_config");
		p_background = new Game_Background(this->p_config);
		p_building = new Game_Building(this->p_config);
		p_block = new Game_Block(this->p_config);
		p_reward = new Game_Reward(this->p_config);
		
		
		p_timer = new UI::UI_Timer(this);
		p_timer->StartTimer(p_config->Game_Timer_Tick,true);
		UI::UI_MessageServer::Instance().RegisterMessageClient(this, UI::UI_Message::CTRL_TIMER, UI::UI_MessageServer::PRIORITY_FIRST);
		UI::UI_MessageServer::Instance().RegisterMessageClient(this, UI::UI_Message::KEYBOARD_KEYDOWN, UI::UI_MessageServer::PRIORITY_FIRST);
	}
	~TestView(){
		delete p_config;
		delete p_block;
		delete p_reward;
		delete p_building;
		delete p_background;
	}
	virtual bool HandleMessage(UI_Message* pMessage){
		bool bHandled = false;
		if(!bHandled && pMessage){
			switch(pMessage->MessageType()){			
			case UI::UI_Message::CTRL_TIMER:{
				if(pMessage->Destination() == this && pMessage->Source() == this->p_timer){
					if(Game_Part::TickAllParts())
						this->Draw();
					bHandled = true;
				}
				break;
			}
			case UI::UI_Message::KEYBOARD_KEYDOWN:{
				UI::UI_KeyboardMessage* p = dynamic_cast<UI::UI_KeyboardMessage*>(pMessage);
				switch(p->Key){
				case SDLK_ESCAPE:
					UI::UI_MessageServer::Instance().QueueMessage(new UI::UI_Message(UI::UI_Message::APP_EXIT, 0, this));
					bHandled = true;
					break;
				default:
					break;
				}
				break;
			}
			default:
				bHandled = UI_View::HandleMessage(pMessage);
				break;
			}
		}
		return bHandled;
	}
};
